package com.bingbaihanji.springbooth2basedemo.controller;


import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bingbaihanji.springbooth2basedemo.pojo.Area;
import com.bingbaihanji.springbooth2basedemo.service.IAreaService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 冰白寒祭
 * @since 2023-12-04
 */
@RestController
@RequestMapping("/area")
@Slf4j
public class AreaController {

    @Resource
    private IAreaService areaService;

    @GetMapping("/province/{province}")
    public List<Area> queryProvince(@PathVariable String province) {

        QueryWrapper<Area> wrapper = new QueryWrapper<>();
        wrapper.like("name", province);
        Area one = areaService.getOne(wrapper);

        QueryWrapper<Area> city = new QueryWrapper<>();
        city.eq("parent_id", one.getAreaId());
        List<Area> list = areaService.list(city);

        String jsonArrayString = JSONArray.toJSONString(list, JSONWriter.Feature.PrettyFormat);
        log.info("\n" + jsonArrayString);
        return list;
    }
}
